#include "rs232.h"

RS232::RS232(QObject *parent)
    : Device{parent} {

}

int RS232::nPort() const
{
    return m_nPort;
}

void RS232::setNPort(int newNPort)
{
    m_nPort = newNPort;
}

int RS232::nBaud() const
{
    return m_nBaud;
}

void RS232::setNBaud(int newNBaud)
{
    m_nBaud = newNBaud;
}

int RS232::nCheck() const
{
    return m_nCheck;
}

void RS232::setNCheck(int newNCheck)
{
    m_nCheck = newNCheck;
}

int RS232::nDatabits() const
{
    return m_nDatabits;
}

void RS232::setNDatabits(int newNDatabits)
{
    m_nDatabits = newNDatabits;
}

int RS232::nStopbits() const
{
    return m_nStopbits;
}

void RS232::setNStopbits(int newNStopbits)
{
    m_nStopbits = newNStopbits;
}

void RS232::writeXML(QXmlStreamWriter& writer){
    // 写入当前操作的信息
    writer.writeStartElement("RS232");
    writer.writeAttribute(QStringLiteral("id"),m_strID);

    writer.writeTextElement("port", QString::number(m_nPort));
    writer.writeTextElement("baud", QString::number(m_nBaud));
    writer.writeTextElement("check", QString::number(m_nCheck));
    writer.writeTextElement("databits", QString::number(m_nDatabits));
    writer.writeTextElement("stopbits", QString::number(m_nStopbits));
    writer.writeEndElement();  // 结束当前操作的元素
}
// 有点问题
void RS232::readFromXML(QXmlStreamReader& reader){
    QXmlStreamAttributes attributes = reader.attributes();
    if(attributes.hasAttribute("id")) {
        m_strID = attributes.value("id").toString();
    }

    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement &&
             reader.name() == QStringLiteral("RS232"))) {

        if (reader.name() == QStringLiteral("port")) {
            m_nPort = reader.text().toInt();
        }else if (reader.name() == QStringLiteral("baud")) {
            m_nBaud = reader.text().toInt();
        }else if (reader.name() == QStringLiteral("check")) {
            m_nCheck = reader.text().toInt();
        }else if (reader.name() == QStringLiteral("databits")) {
            m_nDatabits = reader.text().toInt();
        }else if (reader.name() == QStringLiteral("stopbits")) {
            m_nStopbits = reader.text().toInt();
        }

        reader.readNext();
    }
}
